<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * CKEditor helper for CodeIgniter
 * 
 * @author Samuel Sanchez <samuel.sanchez.work@gmail.com> - http://kromack.com/
 * @package CodeIgniter
 * @license http://creativecommons.org/licenses/by-nc-sa/3.0/us/
 * @tutorial http://kromack.com/developpement-php/codeigniter/ckeditor-helper-for-codeigniter/
 * @see http://codeigniter.com/forums/viewthread/127374/
 * @version 2010-08-28
 * 
 */

/**
 * This function adds once the CKEditor's config vars
 * @author Samuel Sanchez 
 * @access private
 * @param array $data (default: array())
 * @return string
 */
function cke_value($str){
	$CI =& get_instance();
	$data = array(
		'path' => $CI->config->item('base_asset_url').'assets/plugins/ckeditor',
		'cfinder_path' => $CI->config->item('base_asset_url').'assets/plugins/ckfinder'
	);
	return $data[$str];
} 
 
function cke_initialize() {
	
	$return = '';
	
	if(!defined('CI_CKEDITOR_HELPER_LOADED')) {
		
		define('CI_CKEDITOR_HELPER_LOADED', TRUE);
		$return =  '<script type="text/javascript" src="'.cke_value('path') . '/ckeditor.js"></script>';
		$return .=	"<script type=\"text/javascript\">CKEDITOR_BASEPATH = '" .cke_value('path') . "/';</script>";
	} 
	
	return $return;
	
}

/**
 * This function create JavaScript instances of CKEditor
 * @author Samuel Sanchez 
 * @access private
 * @param array $data (default: array())
 * @return string
 */
function cke_create_instance($id,$data = array(),$fileupload=TRUE) {
	
    $return = "<script type=\"text/javascript\">
     	CKEDITOR.replace('" . $id . "', {";
    
    		//Adding config values
    		if(isset($data)) {
	    		foreach($data as $k=>$v) {
	    			
	    			// Support for extra config parameters
	    			if (is_array($v)) {
	    				$return .= $k . " : [";
	    				$return .= config_data($v);
	    				$return .= "]";
	    				
	    			}
	    			else {
	    				$return .= $k . " : '" . $v . "'";
	    			}

	    			//if($k !== end(array_keys($data))) {
						$return .= ",";
					//}		    			
	    		} 
    		}
			//Add ENTER_MODE
			$return .= "enterMode:2,";
	//File Upload Active		
	if(!empty($fileupload)):
	$return .="
                	filebrowserBrowseUrl : '".cke_value('cfinder_path')."/ckfinder.html',
                  	filebrowserImageBrowseUrl : '".cke_value('cfinder_path')."/ckfinder.html?Type=Images',
                  	filebrowserFilesBrowseUrl : '".cke_value('cfinder_path')."/ckfinder.html?Type=Files',
                   	filebrowserUploadUrl : '".cke_value('cfinder_path')."/core/connector/php/connector.php?command=QuickUpload&type=Files',
                 	filebrowserImageUploadUrl : '".cke_value('cfinder_path')."/core/connector/php/connector.php?command=QuickUpload&type=Images'	
	";			
	endif;   			
    				
    $return .= '});</script>';	
    
    return $return;
	
}

/**
 * This function displays an instance of CKEditor inside a view
 * @author Samuel Sanchez 
 * @access public
 * @param array $data (default: array())
 * @return string
 */
function display_ckeditor($id,$value,$data = array(),$fileupload=TRUE)
{
	// Initialization
	$return = cke_initialize();
	
	$return .= '<textarea name="'.$id.'" id="'.$id.'" >'.$value.'</textarea>';
	
    // Creating a Ckeditor instance
    $return .= cke_create_instance($id,$data,$fileupload);	

    return $return;
}